package com.huawei;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: HJ39 判断两个IP是否属于同一子网
 * 若IP地址或子网掩码格式非法则输出1，若IP1与IP2属于同一子网络输出0，若IP1与IP2不属于同一子网络输出2
 *
 * @author weiruibai.vendor
 * Date: 2022/8/26 14:15
 */
public class HJ39 {

    /**
     * 255.0.0.0
     * 193.194.202.15
     * 232.43.7.59
     * <p>
     * 255.256.0.0
     * 130.32.67.107
     * 143.32.132.184
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String mask;
        while ((mask = reader.readLine()) != null
                && mask.length() > 0) {
            String ip1 = reader.readLine();
            String ip2 = reader.readLine();
            if (!valifyMask(mask)) {
                System.out.println("1");
            } else if (!valifyIP(ip1) || !valifyIP(ip2)) {
                System.out.println("1");
            } else {
                valifyIsSameNet(mask, ip1, ip2);
            }
        }

    }

    private static void valifyIsSameNet(String mask, String ip1, String ip2) {
        // 来到这里说明ip1 ip2都是合法的，只考虑是否同一个网段就可以
        String[] masks = mask.split("\\.");
        String[] ip1s = ip1.split("\\.");
        String[] ip2s = ip2.split("\\.");
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int m = Integer.parseInt(masks[i]);
            int i1 = Integer.parseInt(ip1s[i]);
            int i2 = Integer.parseInt(ip2s[i]);
            sb1.append(m & i1);
            sb2.append(m & i2);
        }
        System.out.println((sb1.toString()).equals(sb2.toString()) ? "0" : "2");
    }

    private static boolean valifyIP(String mask) {
        // 四个.分隔符，且都有数据
        String[] strs = mask.split("\\.");
        if (strs.length != 4) {
            return false;
        }
        try {
            for (String s : strs) {
                if (s.equals("")) {
                    return false;
                }
                Integer num = Integer.parseInt(s);
                // 判断是否1～255
                if (num < 0 || num > 255) {
                    return false;
                }
            }
        } catch (Exception e) {
            System.out.println("err");
            return false;
        }
        return true;

    }

    private static boolean valifyMask(String mask) {
        // 四个.分隔符，且都有数据
        String[] strs = mask.split("\\.");
        if (strs.length != 4) {
            return false;
        }
        boolean startZero = false;
        try {
            for (String s : strs) {
                if (s.equals("")) {
                    return false;
                }
                Integer num = Integer.parseInt(s);
                // 判断是否0～255
                if (num < 0 || num > 255) {
                    return false;
                }
                for (int i = 7; i >= 0; i--) { // 8位，且从左往右（高位往低位）
                    int andAns = (num & (1 << i));
                    if (andAns != 0 && startZero) {
                        return false;
                    } else if (andAns == 0 && !startZero) {
                        // 第一次出现0
                        startZero = true;
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
